#include "TPage.h"
#include "TUI.h"
#include "../utils/Utils.h"



TPageBar::TPageBar(){
    
    
    
}

bool TPageBar::init(){
    if(!TPanel::init()){
        return false;
    }
    
    return true; 
};



void TPageBar::setCurPage(int page){
    
}

void TPageBar::setTotalPage(int page){
    
}




TPage::TPage(){
    m_pLeftBtn=NULL;
    m_pRightBtn=NULL;
    m_pPageTxt=NULL;
    m_pBar=NULL;
}

bool TPage::init(){
    if(!TScrollPanel::init()){
        return false;
    }
    nCurPage=0; 
    return true;
}


void TPage::clearItems(){
    TScrollPanel::clearItems();
    gotoPage(1);
}



TPage* TPage::create(xml_node<> * pItem){
    TPage*  ret=TPage::create();
    ret->initWidthConf(pItem);
    return ret;
}


void TPage::initWidthConf(xml_node<> * pItem){
    
    if(!pItem){
        return ;
    }
    
    string scrollDir;
    readAttrString(pItem, "itemName", itemName);
    readAttrString(pItem, "scrollDir", scrollDir);
    
    if(scrollDir=="V"){
        setDirection(TScrollV);
    }else{
        setDirection(TScrollH);
    }
    
    m_nRowNum =readAttrInt(pItem, "row");
    m_nColNum=readAttrInt(pItem, "col");
    m_nRowDistance=readAttrInt(pItem, "rowDis");
    m_nColDistance=readAttrInt(pItem, "colDis");
    nMaxNum=readAttrInt(pItem, "maxNum");
    
    for(uint32 i=0;i<nMaxNum;i++){ 
        TPanelItem* item=TUI::sharedDirector()->createUI(itemName);
        addChildItem(item); 
        TPanel* panel=dynamic_cast<TPanel*>(item);
        if(panel && panel->confPath.size()!=0){
            panel->loadConf(panel->confPath.c_str());
        }
        item->setTag(i+1);
        m_nItemWidth=item->getContentSize().width;
        m_nItemHeight=item->getContentSize().height;
        
        
    } 
    layout();
}

void TPage::layout(){
    for(uint32 i=0;i<gTouchItems->count();i++){
        TPanelItem* item=(TPanelItem*)gTouchItems->objectAtIndex(i);
        item->setVisible(false);
        m_nItemWidth=item->getContentSize().width;
        m_nItemHeight=item->getContentSize().height; 
        int index= i%(m_nColNum*m_nRowNum);
        int curPage=i/(m_nColNum*m_nRowNum);
        int curRow=index/m_nColNum;
        int curCol=index%m_nColNum;
        item->nPage=curPage+1;
        item->setPositionX(  getPageWidth()* curPage+curCol*(m_nItemWidth+ m_nColDistance)-1);
        item->setPositionY(-curRow*(m_nItemHeight+ m_nRowDistance)-1); 
    } 
    m_pContainer->setContentSize(CCSizeMake(getPageWidth()-m_nColDistance+2,getPageHeight()-m_nRowDistance));
    
    checkVisible();
}



void TPage::gotoPage(int curPage ){
    if(nCurPage==curPage){
        
        return;
    }
   
    if(curPage<1){
        curPage=1; 
    }
    
    if(curPage>getTotalPage()){
        curPage=getTotalPage(); 
    }
    
    if(curPage<=1){
        if(m_pLeftBtn){
            m_pLeftBtn->setVisible(false);
        }
       
    }else{
        if(m_pLeftBtn){
            m_pLeftBtn->setVisible(true);
        }
    }
    
    
    if(curPage>=getTotalPage()){
        if(m_pRightBtn){
            m_pRightBtn->setVisible(false);
        }
    }else{
        
        if(m_pRightBtn){
            m_pRightBtn->setVisible(true);
        }
    }
    
    
    
    int start=nCurPage;
    if(nCurPage>curPage){
        start=curPage;
    }
    
    
    for(uint32 i=0;i<gTouchItems->count();i++){
        TPanelItem* item=(TPanelItem*)gTouchItems->objectAtIndex(i);
        item->setVisible(true);
      
    }
    
    
    
    nCurPage=curPage;
    
    m_pContainer->stopAllActions();
    m_pContainer->runAction(
                            CCSequence::create(
                                               CCEaseExponentialOut::create(CCMoveTo::create(0.7f,
                                                                                             ccp( -(getPageWidth())*(nCurPage-1)  ,
                                                                                                 0)
                                                                                             )
                                                                            ),
                                               CCCallFunc::create(this, callfunc_selector(TPage::onPageEnd)),
                                               NULL
                                               )
                            );
    
    
    if(m_pPageTxt){
        string temp=parseString(nCurPage);
        temp.append("/");
        temp.append(parseString(getTotalPage()));
        m_pPageTxt->setString(temp);
    }
    
    if(m_pBar){
        m_pBar->setCurPage(nCurPage);
        m_pBar->setTotalPage(getTotalPage());
        
    }
    
}



void TPage::onPageEnd(){
    checkVisible();
   
}

void TPage::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){
    TScrollPanel::ccTouchMoved(pTouch, pEvent);
    
    
}

TPanelItem* TPage::ensureItemByIdx( int idx )
{
	TPanelItem* item = (TPanelItem*)m_pContainer->getChildByTag(idx);
	if(item != NULL) {
		return item;
	}
	for(uint32 i=nMaxNum;i<idx;i++){ 
		TPanelItem* item=TUI::sharedDirector()->createUI(itemName);
		addChildItem(item); 
		TPanel* panel=dynamic_cast<TPanel*>(item);
		if(panel && panel->confPath.size()!=0){
			panel->loadConf(panel->confPath.c_str());
		}
		item->setTag(i+1);
		m_nItemWidth=item->getContentSize().width;
		m_nItemHeight=item->getContentSize().height;
	}
	nMaxNum = idx;
	layout();
	return getItemByIdx(idx);

}


TPanelItem* TPage::getItemByIdx(int idx){
    return (TPanelItem*)m_pContainer->getChildByTag(idx);
}

void TPage::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){
     
    CCPoint curPos (m_pContainer->getPosition().x,m_pContainer->getPosition().y);
    
    
    CCPoint touchEndPos= pTouch->getLocation();
    
    int distance= (touchEndPos.x-touchStartPos.x);
    
    
    if(distance>8){
        gotoPage(nCurPage-1);
    }else if(distance<-8){
        gotoPage(nCurPage+1);
        
    }else{
        gotoPage(nCurPage);
        TPanel::ccTouchEnded(pTouch, pEvent);
    }
    
    
}


float TPage::getPageWidth(){
    return  m_nColNum*m_nItemWidth+(m_nColNum )*m_nColDistance;
}

float TPage::getPageHeight(){
    return  m_nRowNum*m_nItemHeight+(m_nRowNum )*m_nRowDistance;
}

int TPage::getTotalPage(){
    float aPage=m_nColNum*m_nRowNum;
    return ceilf( gTouchItems->count() /aPage);
}